
(load "edit.lisp")
(load "get-exp.lisp")
(load "lisp-list.lisp")
(load "bb.lisp")
(load "myconfig.lisp")
(load "sh.lisp")
(load "pr.lisp")
(load "cn-if-merge.lisp")
(load "cn-cond-merge.lisp")
(load "cn-travel-merge.lisp")
(load "cn-let-merge.lisp")
(load "cn-prog.lisp")
;(load "中文/基础定义.lisp")
(defvar ! nil)
(defun get-myline()
  (let ((lstr nil)(line nil))
   (loop (when (or (and line (<= (length line) 0))(and lstr (char/= (char lstr (1- (length lstr))) #\\))) (return))
      (setf lstr (read-line))
      (if (> (length lstr) 0)
	  (setf line (concatenate 'string line (subseq lstr 0 (1- (length lstr)))))
	  (setf line "")))
   (if (> (length line) 0)
       (setf line (concatenate 'string line (string (char lstr (1- (length lstr))))))
       "")))

(defun main()
  (do ((line nil)
       (homepath "/home/y/mylisp/shell/2.0")
       (s-exp nil)
       (str nil)
       (c nil)
       (prompt "shell>"))
      ((equal s-exp 'exit)(goodbye))
    (format t "~a" prompt)
    (setf line (get-myline))
    ;(format t "line=[~a] len=[~a]~%" line (length line))
	  (if (> (length line) 0)
	      (progn 
	        (setf line (string-trim '(#\space #\tab) (string-half line)))
		(setf s-exp (read-from-string line))
		(setf c (char line 0))
		(setf str  (format nil "~a" s-exp))
;;		(print c)
		(cond   ((char= c #\( )(progn (prin1 (eval (c-call s-exp))) (format T "~%")) )
			((char= c #\" )(progn (prin1 (eval (e str (if (>= (+ 2 (length str)) (length line)) nil (read-from-string (subseq  line (+ (length str) 2) (length line))))))))(format T "~%"))
			((char= c #\@ )(let ((db "mydb")(tbl "日志")(db-com nil))
				(cond 
					((string= (get-word line) "@insert") (setf db-com (concatenate 'string "insert into " tbl (get-field line) "values" (get-values line))))
					((string= (get-word line) "@update") (setf db-com (concatenate 'string "update " tbl "set " (get-exp line))))
					((string= (get-word line) "@delete") (setf db-com (concatenate 'string "delete from " tbl " where " (get-exp line))))
					((string= (get-word line) "@select") (setf db-com (concatenate 'string "select * from " tbl " where " (get-exp line))))
					(T (format t "不符合数据库操作语法，请重新输入。~%")))
				(if (not (null db-com)) (progn 	(setf db-com (format nil "mysql -e \'~a;\' ~a" db-com db))
							(format t "~a~%"  db-com)
							(sh db-com)
							(format t "~%")))))
			((char= c #\! )(let ((fname (get-word (string-trim '(#\space #\tab) (subseq line 1 (length line))))))
					(if (not (null fname)) (if (< (char-code (char fname 0)) 255) (lisp-list fname) (cn-list fname)))))
			((or (char= c #\[)(> (char-code c) 255))  (bb-proc line))
			(T	(if (string= str "CD")
				    (let ((newpath (string-trim '(#\space #\tab) (left-str line))))
				      (if (string= newpath "")
					  (ext:cd homepath)
					  (ext:cd newpath)))
				    (shell line))))
		(if (and (char= c #\() (and (atom (car s-exp))(char= (char (string (car s-exp)) 0) #\_))) (setf ! s-exp))	
		)
	      )
    ))

(main)
